package chapter02

object Test13_String4 {
  def main(args: Array[String]): Unit = {
    //统计字符串中字符和数字出现的个数
    def wordAndNumberCount(s:String): Unit = {
      var wordCount = 0
      var numberCount = 0
      for(i<-s) {
        if (i.isLetter) wordCount += 1
        if (i.isDigit) numberCount += 1
      }
      println(s"字符的个数：${wordCount},数字的个数：${numberCount}")
    }
    wordAndNumberCount("asdhjj789f sdf")
    def wordAndNumberCount1(s:String): Unit = {
//      var wordCount = 0
//      var numberCount = 0
//      s.foreach(e=>{if(e.isLetter) wordCount+=1})
//      s.foreach(e=>{if(e.isDigit) numberCount+=1})
      val wordCount = s.filter(_.isLetter).length
      val numberCount = s.count(_.isDigit)
      println(s"字符的个数：${wordCount},数字的个数：${numberCount}")
    }
    wordAndNumberCount1("asdhjj789f sdf")
  }
}
